import { assign } from 'lodash'
import {
  fetchTableData,
  fetchTreeMap,
  fetchMeterDetail,
  fetchEHeatmapData,
  fetchDayDetail,
  fetchYqglPieData
} from '@/api/meterReadingOptimize'

const defaultState = () => {
  return {
    defaultDate: undefined,
    tableData: null,
    tableLoading: false,
    tableHint: '',
    tablePageParams: {
      pageNum: 1,
      pageSize: 20
    },
    userQueryParams: {
      unitId: undefined,
      unitName: undefined
    },
    stopTableKeyUp: false,
    treeMapList: null,
    currentMeterId: undefined,
    currentGasCls: undefined,
    collapseLeftFlag: false,
    collapseRightFlag: false,
    meterDetail: null,
    eHeatmapList: [],
    eHeatmapLoading: false,
    yqglData: null,
    rightTopLoading: false,
    yqglPieData: null,
    selectAreaFlag: true
  }
}

export default {
  namespaced: true,
  name: 'meterReadingOptimize',
  state: defaultState(),
  getters: {
    getTablePageParams: state => {
      return {
        ...state.tablePageParams
      }
    }
  },
  mutations: {
    /**
     * desc: 设置表格数据
     * param {type}
     * return {type} void
     */
    SET_TABLE_DATA(state, tableData) {
      state.tableData = tableData
    },
    /**
     * desc: 设置表格加载状态
     * param {type}
     * return {type} void
     */
    SET_TABLE_LOADING(state, flag) {
      state.tableLoading = flag
    },
    /**
     * desc: 设置表格请求页数
     * param {type}
     * return {type} void
     */
    SET_TABLE_PAGE_NUM(state, num) {
      state.tablePageParams.pageNum = num
    },
    /**
     * desc: 设置表格键盘操作标识符
     * param {type}
     * return {type} void
     */
    SET_STOP_TABLE_KEYUP(state, flag) {
      state.stopTableKeyUp = flag
    },
    /**
     * desc: 设置矩形树图数据
     * param {type}
     * return {type} void
     */
    SET_TREEMAP_DATA(state, data) {
      state.treeMapList = data
    },
    SET_CURRENT_METERID(state, meterId) {
      state.currentMeterId = meterId
    },
    SET_COLLAPSE(state, { type, status }) {
      if (type === 'left') {
        state.collapseLeftFlag = status
      } else if (type === 'right') {
        state.collapseRightFlag = status
      }
    },
    SET_METER_DETAIL(state, data) {
      state.meterDetail = data
    },
    REST_STATE(state) {
      state = assign(state, defaultState())
    },
    SET_CURRENT_GAS_CLS(state, type) {
      state.currentGasCls = type
    },
    SET_USER_QUERY_PARAMS(state, data) {
      state.userQueryParams = {
        ...state.userQueryParams,
        ...data
      }
    },
    SET_EHEATMAP_LIST(state, list) {
      state.eHeatmapList = list
    },
    SET_EHEATMAP_LOADING(state, flag) {
      state.eHeatmapLoading = flag
    },
    SET_YQGL_DATA(state, data) {
      state.yqglData = data
    },
    SET_YQGL_PIE_DATA(state, data) {
      state.yqglPieData = data
    },
    SET_RIGHT_TOP_LOADING(state, flag) {
      state.rightTopLoading = flag
    },
    SET_SELECTE_AREA_FLAG(state, flag) {
      state.selectAreaFlag = flag
    }
  },
  actions: {
    /**
     * desc: 获取表格数据
     * param {type}
     * return {type} void
     */
    async loadTableData(ctx, params) {
      ctx.commit('SET_TABLE_LOADING', true)
      const _tableData = await fetchTableData({
        ...ctx.state.tablePageParams,
        ...params
      })
      ctx.commit('SET_TABLE_LOADING', false)
      const { code, date = {} } = _tableData
      if (code === 200) {
        ctx.commit('SET_TABLE_DATA', date)
      }
    },
    /**
     * desc: 修改表格页码
     * param {type}
     * return {type} void
     */
    changeTablePageNum(ctx, num) {
      ctx.commit('SET_TABLE_PAGE_NUM', num)
    },
    /**
     * desc: 是否允许表格键盘操作
     * param {type}
     * return {type} void
     */
    changeStopTableKeyUp(ctx, flag) {
      ctx.commit('SET_STOP_TABLE_KEYUP', flag)
    },
    /**
     * desc: 获取矩形树图
     * param {type}
     * return {type} void
     */
    async loadTreeMapData(ctx) {
      const { code, date } = await fetchTreeMap()
      if (code === 200) {
        ctx.commit('SET_TREEMAP_DATA', date)
      }
    },
    changeCurrentMeterId(ctx, meterId) {
      ctx.commit('SET_CURRENT_METERID', meterId)
    },
    changeCollapse(ctx, arg) {
      ctx.commit('SET_COLLAPSE', arg)
    },
    async lodaMeterDetail(ctx) {
      const { code, date } = await fetchMeterDetail(ctx.state.currentMeterId)
      if (code === 200) {
        ctx.commit('SET_METER_DETAIL', date)
      }
    },
    clearStore(ctx) {
      ctx.commit('REST_STATE')
    },
    changeCurrentGasCls(ctx, type) {
      ctx.commit('SET_CURRENT_GAS_CLS', type)
    },
    changeUserQueryParams(ctx, data) {
      ctx.commit('SET_USER_QUERY_PARAMS', data)
    },
    async loadingEHeatmapList(ctx, params) {
      ctx.commit('SET_EHEATMAP_LOADING', true)
      const { code, date } = await fetchEHeatmapData(params)
      ctx.commit('SET_EHEATMAP_LOADING', false)
      if (code === 200) {
        ctx.commit('SET_EHEATMAP_LIST', date)
      }
    },
    async loadingYqglList(ctx, params) {
      ctx.commit('SET_RIGHT_TOP_LOADING', true)
      ctx.commit('SET_YQGL_PIE_DATA', null)
      const { code, date } = await fetchDayDetail(params)
      ctx.commit('SET_RIGHT_TOP_LOADING', false)
      if (code === 200) {
        ctx.commit('SET_YQGL_DATA', date)
      }
    },
    async loadYqglPieData(ctx, params) {
      ctx.commit('SET_RIGHT_TOP_LOADING', true)
      const {
        code,
        date
      } = await fetchYqglPieData(params)
      ctx.commit('SET_RIGHT_TOP_LOADING', false)
      if (code === 200) {
        ctx.commit('SET_YQGL_PIE_DATA', date)
      }
    },
    changeSelectAreaFlag(ctx, flag) {
      ctx.commit('SET_SELECTE_AREA_FLAG', flag)
    }
  }
}
